<?php
/*
*	Template Name: Contact Us
*/
require_once('inc/recaptchalib.php');
$private_key = '6LfMq-ASAAAAANZ3emCL-OYdDM3ktS_gGY2WiMAV';
$options = get_option( 'dmci_theme_options' ); 
$success = FALSE;

if( isset($_POST['submit']) ){
	
	$values = array();	
	$errors = array();
	$fields = array('your_name', 'contact_no', 'email', 'message');
	
	foreach($_POST as $key => $val){
		if( in_array($key, $fields) ){
			if( $val == '' ) {
				$errors[] = ucfirst(str_replace('_', ' ', $key)) . ' field is required';
			}
			if( $key == 'email' && $val != ''){
				if( !validate_email($val) ){
					$errors[] = 'Please enter a valid email address';
				}
			}
			
			$values[$key] = $val;
		}
	}
	
	
	$resp = recaptcha_check_answer ($private_key, $_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
	
	if (!$resp->is_valid) {
		$errors[] = 'ReCaptcha field invalid. Try again';
	}
	
	if( empty($errors) ){
		
		$subject = 'New Inquiry';
		$myemail = $options['user_email'];
		
		$fullmessage = '
			A new person has filled up the contact form on your website with the following information: <br/><br/>
			
			Name: '.strip_tags($_POST['your_name']).' <br/>
			Contact No: '.strip_tags($_POST['contact_no']).' <br/>
			Email: '.strip_tags($_POST['email']).' <br/>
			Message:  <br/>'.strip_tags($_POST['message']).'
		';
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= 'From: Contact Form  <'.$options['user_email'].'>' . "\r\n";
		$header .= "Reply-To: <no-replay@dmci.com> \r\n" .
		mail($myemail, $subject, $fullmessage, $headers);
		
		unset($values);
		$success = TRUE;
	}
	
}
	
?>
<?php get_header(); ?>
 

<div class="container" id="top_info">
	<div class="top-content">&nbsp;</div>
    <div class="top-curve">&nbsp;</div>
</div>

<div class="container">
	<?php 
		$sliders = get_post_meta(get_the_ID(), 'dmci_page_slider', true);
		$slider_title = get_post_meta($post->ID, 'dmci_page_slider_title', true);
		$slider_desc = get_post_meta($post->ID, 'dmci_page_slider_desc', true);

		if( !empty($sliders) ):
		$sliders = json_decode(htmlspecialchars_decode($sliders), true);
		
	?>
	

	<div class="page-slider-wrapper">
    <div class="page-slider">
		
		<?php foreach($sliders as $slide) :?>
        <div class="slide">
        	<img src="<?php echo $slide; ?>" alt="image1" />
        </div>		
		<?php endforeach;?>
        
    </div>
	<div class="slider-text">
        <div class="title"><?php echo $slider_title; ?></div>
     	<div class="desc"><?php echo $slider_desc; ?></div>
    </div>
    </div>
	<?php else: ?>
		<div class="page-filler">&nbsp;</div>
	<?php endif; ?>
	
	<div class="page-content padded">
		
        <?php while ( have_posts() ) : the_post(); ?>

		<?php the_content(); ?>

		<?php endwhile; // end of the loop. ?>
        
		<?php if($success) :?>
		<div class="success-msg">
			<h3>Thank you, your message has been sent. </h3>
		</div>
		<?php endif; ?>
       
	   	<div class="contact_info">
			<?php 
				$options = get_option( 'dmci_theme_options' );  
				echo !empty($options) ? apply_filters("the_content", $options['contact_info']) : '';
			
			?>
		</div>
		<form method="post" action="<?php echo the_permalink(); ?>" id="contact_form">
			<?php
				if( isset($errors) ){
					echo '<div class="form-error">';
					for( $x = 0; $x < count($errors); $x++){
						echo $errors[$x] . '<br/>';
					}
					echo '</div>';
				}
			
			?>
			 <?php 
			
			
			if( empty($options) || $options['user_email'] == '' ):
			?>
			No email address defined. Please login to your admin account and fill up the email field under "Appereance > Theme Options".
			<?php endif;?>
			
			<div class="block">
			<label for="your_name">Your Name *</label>
			<input type="text" name="your_name" id="your_name" value="<?php echo isset($values['your_name']) ? $values['your_name'] : '';?>"/>
			</div>
			
			<div class="block">
			<label for="contact_no">Contact No. *</label>
			<input type="text" name="contact_no" id="contact_no" value="<?php echo isset($values['contact_no']) ? $values['contact_no'] : '';?>"/>
			</div>
			
			<div class="block">
			<label for="email">Email *</label>
			<input type="text" name="email" id="email" value="<?php echo isset($values['email']) ? $values['email'] : '';?>" />
			</div>
			
			<div class="block">
			<label for="message">Message *</label>
			<textarea name="message" id="message"><?php echo isset($values['message']) ? $values['message'] : '';?></textarea>
			</div>
			
			<div class="captcha-block">
			<?php
			  
			  $publickey = "6LfMq-ASAAAAAITWsmmi-Dhl2ziXX3SvPIQMtZlu"; // you got this from the signup page
			  echo recaptcha_get_html($publickey);
			?>
			</div>
			
			<div class="clear">&nbsp;</div>
			
			<input type="submit" value="Submit" name="submit" />
			
			
		</form>
		
		 
		<div class="clear">&nbsp;</div>	
	</div>
	<div class="clear">&nbsp;</div>
</div>

<?php get_footer(); ?>